import spacy

# 加载预训练的spaCy模型
nlp = spacy.load("en_core_web_sm")

# 用户提问
question = "Who is the author of 'To Kill a Mockingbird'?"

# 问题解析
doc = nlp(question)
for token in doc:
    print(f"Token: {token.text}, POS: {token.pos_}, Dependency: {token.dep_}")


from elasticsearch import Elasticsearch

# 初始化Elasticsearch客户端
es = Elasticsearch(["http://localhost:9200"])

# 构造查询语句（以关键词搜索为例）
query = {
    "query": {
        "match": {
            "content": "To Kill a Mockingbird author"
        }
    }
}

# 执行查询
response = es.search(index="my_index", body=query)

# 处理查询结果
for hit in response['hits']['hits']:
    print(hit["_source"])


# 假设response['hits']['hits']中已包含相关条目
answer = ""
for hit in response['hits']['hits']:
    # 假设每个条目都有一个"answer"字段直接包含答案
    if "answer" in hit["_source"]:
        answer += hit["_source"]["answer"] + "\n"

# 输出最终答案
print("Answer:")
print(answer.strip())

